package com.eascs.admin.o2o.recommend.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.admin.o2o.internal.util.BaseUtils;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.recommend.entity.O2oRecommendEntity;
import com.eascs.admin.o2o.recommend.service.O2oRecommendService;
import com.eascs.admin.o2o.recommend.vo.O2oRecommendVo;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxConfirmButton;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.tag.MenuTag;
import com.eascs.web.context.RequestContext;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.service.sys.SysOrgService;

@Controller
@RequestMapping("/recommend")
public class O2oRecommendController extends BaseMasterViewController {

	private static Logger log = LoggerFactory.getLogger(O2oRecommendController.class);
	
	@Autowired
	O2oRecommendService o2oRecommendService;

	@Reference
	SysOrgService service;
	
	/**
	 * @Title: index
	 * @Description: 客户管理-客户推荐管理
	 * @author yy
	 * @return
	 * @return String
	 * @throws @date
	 *             2018年2月27日 下午20:17:31
	 */
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index() {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "o2oRecommend");
		//List<ActOrgEntity> orgProvince = service.getOrgProvince();
		List<ActOrgEntity> orgProvince = service.getOrgProvinceAndHQ();
		ViewContext.put("ActOrgEntitys", orgProvince);
		return "recommend/addRecommend";

	}
	
	/**
	 * 新增客户管理
	 */
	@RequestMapping(value="/toadd")
	public String toAddloanapply(String sqType){
		
		User user = UserHolder.getUser();
		List<ActOrgEntity> orgProvince = service.getOrgProvince();
	//	List<ActOrgEntity> orgProvince1 = service.getOrgProvinceAndHQ(); //带总部的省区接口数据
		
		ViewContext.put("ActOrgEntitys", orgProvince);
		O2oRecommendEntity o2oRecommendEntity = new O2oRecommendEntity();
		o2oRecommendEntity.setSqType(sqType);
		if(StatusContexts.RECOMMENDSQ_TYPE_O.equals(sqType)){
			o2oRecommendEntity.setOperatorNo(user.getNo());
			o2oRecommendEntity.setOperatorName(user.getCname());
			o2oRecommendEntity.setOperatorProvinceId(user.getOrg());
			ActOrgEntity  actOrgEntity = service.getOrgByIds(user.getOrg());
			o2oRecommendEntity.setOperatorName(actOrgEntity.getOrgName());
		}
		if(StatusContexts.RECOMMENDSQ_TYPE_R.equals(sqType)){
			o2oRecommendEntity.setReferrerNo(user.getNo());
			o2oRecommendEntity.setReferrerName(user.getCname());
			o2oRecommendEntity.setReferrerProvinceId(user.getOrg());
			/*ActOrgEntity  actOrgEntity = service.getOrgByIds(user.getOrg());
			o2oRecommendEntity.setReferrerName(actOrgEntity.getOrgName());*/
		}
		ViewContext.put("o2oRecommendEntity", o2oRecommendEntity);
		return "recommend/addRecommend";
	}
	
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String search(O2oRecommendVo o2oRecommendVo) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "rec_liebiao");
		o2oRecommendVo.setPageSize(BaseUtils.PAGE_SIZE);
		
	
		PageResult<Object[]> pageResult = o2oRecommendService.findByPage(o2oRecommendVo);
		String[] titles = {"单号", "客户姓名", "身份证号", "客户联系方式", "推荐人","推荐省份", "经办人", "经办省区","推荐类型","共享开始时间","审核状态","是否作废"};
		SelectDataOption[] selectDataOptions = {null, null,  null, null,null,null,null,null,new SelectDataOption("recommendType"),null,new SelectDataOption("LCZT"),new SelectDataOption("RECOMMENDIS_TYPE")};
		
		String pageUrl = UrlUtil.resetParam(RequestContext.getRequestUrlWithQueryString(), "pageIndex", "");
		
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, pageUrl.toString());
		paginationInfo.setSelectDataOptions(selectDataOptions);
		Button[] titleButton = new Button[] { ButtonHelper.createRedirectButton(null, "/recommend/detail?id=") };
		paginationInfo.setTitleButtons(titleButton);
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		ViewContext.put("paginationInfo", paginationInfo);
		ViewContext.put("o2oRecommendVo", o2oRecommendVo);
		return "recommend/list";
	}
	
	
	/**
	 * 详情页面
	 * 
	 */
	@RequestMapping(value = "/detail")
	public String detail(String id) {
		List<ActOrgEntity> orgProvince = service.getOrgProvinceAndHQ();
		ViewContext.put("ActOrgEntitys", orgProvince);
		O2oRecommendEntity o2oRecommendEntity  = o2oRecommendService.getById(id);
		
		ViewContext.put("o2oRecommendEntity", o2oRecommendEntity);
		
		Button button = ButtonHelper.createAjaxButton("启动流程", "/work/recommendStart?loanapplyNo=" + o2oRecommendEntity.getId());
		button.setPermissionCode("RecommendStartFlowButton");
		//*****loan-12273 征信查询单据删除功能 start
		AjaxConfirmButton delBtn = new AjaxConfirmButton("删除",  "/rrecommend/delete?id=" + o2oRecommendEntity.getId()+"&type=D");
		delBtn.setPermissionCode("loanManageDeleteRecommend");
		delBtn.setConfirmMessage("删除数据不可恢复，确认删除？");
		
		AjaxConfirmButton ZFBtn = new AjaxConfirmButton("作废",  "/rrecommend/delete?id=" + o2oRecommendEntity.getId()+"&type=T");
		//ZFBtn.setPermissionCode("loanManageZFRecommend");
		ZFBtn.setConfirmMessage("作废数据不可恢复，确认作废？");
		
		List<Button> btnS = new ArrayList<Button>();
		btnS.add(button);
		btnS.add(delBtn);
		//btnS.add(ZFBtn);
		ViewContext.put("buttons", btnS);
		ViewContext.put("ZFBtn", ZFBtn);
		return "recommend/recommendDetail";
	}
	
	/**
	 * @Title: search
	 * @Description: 编辑客户推荐信息
	 * @author yy
	 * @throws @date 2018年3月1日 下午20:17:44
	 */
	@RequestMapping(value = "/search")
	public String search( String id) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "recommend_search");
		if (StringUtil.isNotBlank(id)) {
			id = id.trim();
		}
		List<ActOrgEntity> orgProvince = service.getOrgProvinceAndHQ();
		ViewContext.put("ActOrgEntitys", orgProvince);
		O2oRecommendEntity o2oRecommendEntity  = o2oRecommendService.getById(id);
		
		ViewContext.put("o2oRecommendEntity", o2oRecommendEntity);
		
		return "recommend/addRecommend";
	}
	
}
